package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.plugins.entity.Comment;
import cn.jdy.plugins.service.CommentService;

/***
 * @desc   评论控制类
 * @author lph
 * @date   2016-8-16
 */
@Controller
@RequestMapping("/comment")
public class CommentCtrl extends BaseCtrl {
	@Autowired
	private CommentService commentservice;
	
	//默认列表路径
    private static String viewName = "redirect:/comment/list?s=";
	/**
	 * 初始化
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(Comment entity, String fromDate, String toDate, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/operate/commentList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
		mv.addObject("entity", entity);
		mv.addObject("fromDate", fromDate);
		mv.addObject("toDate", toDate);
		return mv;
	}
	
	/**
	 * 异步查询
	 * @param news
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(Comment comment, String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", commentservice.query(comment, fromDate, toDate));
			jsonMap.put("iTotalDisplayRecords", commentservice.count(comment, fromDate, toDate));
			jsonMap.put("iTotalRecords", commentservice.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}
	
	
	/**
	 * 编辑/查看
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/operate/commentDetail.jsp");
		mv.addObject("data", id==null||id==0 ? id : this.commentservice.find(id));
		return mv;
	}
	
	/***
	  * @desc  加载所有评论
	  * @author lph
	  */
	 @RequestMapping("/queryAllComment")
	 @ResponseBody
	 public Map<String, Object> queryAllComment(Comment comm){
		 Map<String, Object> jsonMap = new HashMap<String, Object>();
		 try {
			jsonMap.put("dataList", commentservice.querypage(comm));
			jsonMap.put("iTotalDisplayRecords",commentservice.count(comm));
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		 
		 return jsonMap; 
	 }
	/**
	 * 保存（更新）评论
	 * @param news
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(Comment cmt, Long pid) {
		ModelAndView mv = new ModelAndView();
		try {
				//更新
				if(cmt.getCreateTime() == null || cmt.getCreateTime() == ""){
					cmt.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				}
				this.commentservice.update(cmt);
				mv.setViewName(this.viewName.concat("2"));
		    // 清理文章缓存
		    NginxPurgeCacheUtil.purge(2, cmt.getId());
//			}
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "保存异常！", e);
		}

		return mv;
	}
	
	/**
	 * 删除评论
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.commentservice.delete(id);
			mv.setViewName(this.viewName.concat("3"));
			// 清理文章缓存
			NginxPurgeCacheUtil.purge(2, id);
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "删除异常！", e);
		}
		return mv;
	} 
	
	/***
	 * @author Administrator
	 * @param comment
	 * @desc  发布评论
	 */
	@RequestMapping(value = "/addComment", method = RequestMethod.POST)
	@ResponseBody
	public  Map<String, Object> addComment(Comment comment){
        Map<String, Object> jsonMap = new HashMap<String, Object>();
		/*Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置显示格式
		String nowTime="";
		nowTime= df.format(dt);//用DateFormat的format()方法在dt中获取并以yyyy/MM/dd HH:mm:ss格式显示*/
		try {
			//comment.setCreationTime(nowTime);
			comment.setStatus("0");
			commentservice.save(comment);
			jsonMap.put("status", "发布成功");
		} catch (Exception e) {
			// TODO: handle exception
			BaseLog.e(this.getClass(), "addComment", e);
		}
		return jsonMap;
	}
}
